package com.scqkxx.gamebox.activity.myself.spaceUtils;

import android.app.Activity;
import android.content.Context;
import android.icu.text.UFormat;
import android.util.Log;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.scqkxx.gamebox.activity.myself.MyselfInforActivity;
import com.scqkxx.gamebox.bean.EvenBusBean;
import com.scqkxx.gamebox.utils.DialogUtils;
import com.scqkzqtz.base.library.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 空间模块的工具类
 */
public class SpaceUtils {

    /**
     * 删除
     *
     * @param spaceType
     * @param activity
     * @param objectid
     */
    public static void deleteSpaceAndGoods(int spaceType, Activity activity, String objectid) throws AVException {
        if (spaceType == 1) {
            //空间删除
            AVQuery<AVObject> avObjectAVQuery = new AVQuery<>("Space");
            avObjectAVQuery.whereEqualTo("objectId", objectid);
            avObjectAVQuery.whereEqualTo("isDisable", 0);
            avObjectAVQuery.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> avObjects, AVException avException) {
                    if (avException == null) {
                        AVObject avObject = avObjects.get(0);
                        avObject.put("isDisable", 1);
                        avObject.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(AVException e) {
                                if (e == null) {
                                    sendNotify(spaceType, activity);
                                } else {
                                    ToastUtils.showShortToast(activity, "删除失败..");
                                }
                            }
                        });
                    }
                }
            });
        } else if (spaceType == 2) {
            //位置删除
            AVQuery<AVObject> avObjectAVQuery = new AVQuery<>("Position");
            avObjectAVQuery.whereEqualTo("objectId", objectid);
            avObjectAVQuery.whereEqualTo("isDisable", 0);
            avObjectAVQuery.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> avObjects, AVException avException) {
                    if (avException == null) {
                        AVObject avObject = avObjects.get(0);
                        avObject.put("isDisable", 1);
                        avObject.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(AVException e) {
                                if (e == null) {
                                    sendNotify(spaceType, activity);
                                } else {
                                    ToastUtils.showShortToast(activity, "删除失败..");
                                }
                            }
                        });
                    }
                }
            });
        } else {
            //物品删除
            AVQuery<AVObject> avObjectAVQuery = new AVQuery<>("Goods");
            avObjectAVQuery.whereEqualTo("objectId", objectid);
            avObjectAVQuery.whereEqualTo("isDisable", 0);
            avObjectAVQuery.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> avObjects, AVException avException) {
                    if (avException == null) {
                        AVObject avObject = avObjects.get(0);
                        avObject.put("isDisable", 1);
                        avObject.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(AVException e) {
                                if (e == null) {
                                    sendNotify(spaceType, activity);
                                } else {
                                    ToastUtils.showShortToast(activity, "删除失败..");
                                }
                            }
                        });
                    }
                }
            });
        }
    }

    public static void sendNotify(int spaceType, Activity activity) {
        if (spaceType == 1) {
            EvenBusBean evenBusBean = new EvenBusBean(201);
            EventBus.getDefault().post(evenBusBean);
        } else if (spaceType == 2) {
            // 更新位置信息和空间信息详情
            EvenBusBean evenBusBean = new EvenBusBean(101);
            EventBus.getDefault().post(evenBusBean);
        } else {
            EvenBusBean evenBusBean = new EvenBusBean(101);
            EventBus.getDefault().post(evenBusBean);
        }
        activity.finish();
    }

    //添加
    public static boolean insterSpaceAndGoods(int spaceType, Activity context, String spaceName, boolean isSwitch, String photoUrl, int belongGroupId ,String mRemarks) {
        if (spaceType == 1) {
            //空间增加
            AVObject avObject = new AVObject("Space");
            avObject.put("name", spaceName);
            avObject.put("isPublic", isSwitch ? 1 : 0);
            avObject.put("avatar", photoUrl);
            avObject.put("belongGroupId", belongGroupId);
            DialogUtils.showDialog(context);
            avObject.saveInBackground(new SaveCallback() {
                @Override
                public void done(AVException e) {
                    DialogUtils.dismiss();
                    if (e == null) {
                        EvenBusBean evenBusBean = new EvenBusBean(201);
                        EventBus.getDefault().post(evenBusBean);
                        context.finish();
                    } else {
                        Log.e("TAG", "创建空间 ; " + e.getMessage());
                        ToastUtils.showShortToast(context, "创建空间失败..");
                    }
                }
            });
        } else if (spaceType == 3) {
            //位置增加
            AVObject avObject = new AVObject("Position");
            avObject.put("name", spaceName);
            avObject.put("isPublic", isSwitch ? 1 : 0);
            avObject.put("avatar", photoUrl);
            avObject.put("spaceId", belongGroupId);
            avObject.saveInBackground(new SaveCallback() {
                @Override
                public void done(AVException e) {
                    DialogUtils.dismiss();
                    if (e == null) {
                        EvenBusBean evenBusBean = new EvenBusBean(101);
                        EventBus.getDefault().post(evenBusBean);
                        context.finish();
                    } else {
                        Log.e("TAG", "创建空间 ; " + e.getMessage());
                        ToastUtils.showShortToast(context, "图片上传失败..");
                    }
                }
            });
        } else if (spaceType == 5) {
            //物品增加
            AVObject avObject = new AVObject("Goods");
            avObject.put("name", spaceName);
            avObject.put("isPublic", isSwitch ? 1 : 0);
            avObject.put("avatar", photoUrl);
            avObject.put("positionId", belongGroupId);
            avObject.put("note", mRemarks);
            avObject.put("type", 1);
            avObject.saveInBackground(new SaveCallback() {
                @Override
                public void done(AVException e) {
                    DialogUtils.dismiss();
                    if (e == null) {
                        EvenBusBean evenBusBean = new EvenBusBean(101);
                        EventBus.getDefault().post(evenBusBean);
                        context.finish();
                    } else {
                        Log.e("TAG", "创建空间 ; " + e.getMessage());
                        ToastUtils.showShortToast(context, "图片上传失败..");
                    }
                }
            });
        } else if (spaceType == 2) {
            //编辑空间
            DialogUtils.showDialog(context);
            AVQuery<AVObject> avObjectAVQuery = new AVQuery<>("Space");
            avObjectAVQuery.whereEqualTo("objectId", belongGroupId);
            avObjectAVQuery.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> avObjects, AVException avException) {
                    if (avException == null) {
                        if (avObjects.size() > 0) {
                            AVObject avObject = avObjects.get(0);
                            avObject.put("name", spaceName);
                            avObject.put("isPublic", isSwitch ? 1 : 0);
                            avObject.put("avatar", photoUrl);
                            avObject.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(AVException e) {
                                    if (e == null) {
                                        EvenBusBean evenBusBean = new EvenBusBean(101);
                                        EventBus.getDefault().post(evenBusBean);
                                        context.finish();
                                    } else {
                                        Log.e("TAG", "创建空间 ; " + e.getMessage());
                                        ToastUtils.showShortToast(context, "保存失败..");
                                    }
                                    DialogUtils.dismiss();
                                }
                            });
                        }
                    } else {
                        Log.e("TAG", "创建空间 ; " + avException.getMessage());
                        ToastUtils.showShortToast(context, "保存失败..");
                    }
                }
            });
        } else if (spaceType == 4) {
            //编辑位置
            DialogUtils.showDialog(context);
            AVQuery<AVObject> avObjectAVQuery = new AVQuery<>("Position");
            avObjectAVQuery.whereEqualTo("objectId", belongGroupId);
            avObjectAVQuery.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> avObjects, AVException avException) {
                    if (avException == null) {
                        if (avObjects.size() > 0) {
                            AVObject avObject = avObjects.get(0);
                            avObject.put("name", spaceName);
                            avObject.put("isPublic", isSwitch ? 1 : 0);
                            avObject.put("avatar", photoUrl);
                            avObject.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(AVException e) {
                                    if (e == null) {
                                        EvenBusBean evenBusBean = new EvenBusBean(101);
                                        EventBus.getDefault().post(evenBusBean);
                                        context.finish();
                                    } else {
                                        Log.e("TAG", "创建空间 ; " + e.getMessage());
                                        ToastUtils.showShortToast(context, "保存失败..");
                                    }
                                    DialogUtils.dismiss();
                                }
                            });
                        }
                    } else {
                        Log.e("TAG", "创建空间 ; " + avException.getMessage());
                        ToastUtils.showShortToast(context, "保存失败..");
                    }
                }
            });
        }else if( spaceType == 6){
            //物品编辑
            DialogUtils.showDialog(context);
            AVQuery<AVObject> avObjectAVQuery = new AVQuery<>("Goods");
            avObjectAVQuery.whereEqualTo("objectId", belongGroupId);
            avObjectAVQuery.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> avObjects, AVException avException) {
                    if (avException == null) {
                        if (avObjects.size() > 0) {
                            AVObject avObject = avObjects.get(0);
                            avObject.put("name", spaceName);
                            avObject.put("isPublic", isSwitch ? 1 : 0);
                            avObject.put("avatar", photoUrl);
                            avObject.put("note", mRemarks );
                            avObject.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(AVException e) {
                                    if (e == null) {
                                        EvenBusBean evenBusBean = new EvenBusBean(101);
                                        EventBus.getDefault().post(evenBusBean);
                                        context.finish();
                                    } else {
                                        Log.e("TAG", "创建空间 ; " + e.getMessage());
                                        ToastUtils.showShortToast(context, "保存失败..");
                                    }
                                    DialogUtils.dismiss();
                                }
                            });
                        }
                    } else {
                        Log.e("TAG", "创建空间 ; " + avException.getMessage());
                        ToastUtils.showShortToast(context, "保存失败..");
                    }
                }
            });

        }
        return true;
    }


    public static AVObject insterGoods(Activity activity, String spaceName, boolean isSwitch, String objectId, String coordinate) {
        AVObject avObject = new AVObject("Goods");
        avObject.put("name", spaceName);
        avObject.put("isPublic", isSwitch ? 1 : 0);
        avObject.put("avatar", "");
        avObject.put("positionId", objectId);
        avObject.put("type", 2);
        avObject.put("coordinate", coordinate);
        avObject.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    Log.e("TAG" , avObject.toString());
                    EvenBusBean evenBusBean = new EvenBusBean(101);
                    EventBus.getDefault().post(evenBusBean);
                } else {
                    ToastUtils.showShortToast(activity, "位置上传失败上传失败..");
                }
            }
        });
        return avObject;
    }

    /**
     * 更新数据
     * @param activity
     * @param spaceName
     * @param isSwitch
     * @param objectId
     * @param coordinate
     */
    public static void updateGoods(Activity activity, String spaceName, boolean isSwitch, String objectId,int isDisable) {
        AVQuery<AVObject> avObject = new AVQuery("Goods");
        avObject.whereEqualTo("objectId", objectId);
        avObject.whereEqualTo("isDisable", 0);
        avObject.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                if (avException == null){
                    if (avObjects.size() > 0) {
                        AVObject avObject1 = avObjects.get(0);
                        Log.e("TAG" , avObject1.toString());
                        avObject1.put("name", spaceName);
                        avObject1.put("isPublic", isSwitch ? 1 : 0);
                        avObject1.put("avatar", "");
                        avObject1.put("positionId", objectId);
                        avObject1.put("type", 2);
                        avObject1.put("isDisable", isDisable);
                        avObject1.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(AVException e) {
                                if (e == null) {
                                    EvenBusBean evenBusBean = new EvenBusBean(101);
                                    EventBus.getDefault().post(evenBusBean);
                                } else {
                                    ToastUtils.showShortToast(activity, "位置上传失败上传失败..");
                                }
                            }
                        });
                    }
                }else{
                    Log.e("TAG" , avException.getMessage());
                }
            }
        });
    }
}
